home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_21108.txt < prev    next >
Text File  |  1990-04-17  |  12KB  |  381 lines

  1. -- card: 21108 from stack: in.3
  2. -- bmap block id: 0
  3. -- flags: 4000
  4. -- background id: 3241
  5. -- name: TextStream
  6. ----- HyperTalk script -----
  7. on Install
  8.   get ChooseTargetStack()
  9.   InstallResource XCMD,TextStream,it
  10. end Install
  11.  
  12.  
  13. -- part 1 (field)
  14. -- low flags: 81
  15. -- high flags: 2007
  16. -- rect: left=12 top=26 right=298 bottom=491
  17. -- title width / last selected line: 0
  18. -- icon id / first selected line: 0 / 0
  19. -- text alignment: 0
  20. -- font id: 22
  21. -- text size: 10
  22. -- style flags: 0
  23. -- line height: 13
  24. -- part name: Source
  25.  
  26.  
  27. -- part 2 (button)
  28. -- low flags: 00
  29. -- high flags: A003
  30. -- rect: left=57 top=300 right=322 bottom=214
  31. -- title width / last selected line: 0
  32. -- icon id / first selected line: 0 / 0
  33. -- text alignment: 1
  34. -- font id: 0
  35. -- text size: 12
  36. -- style flags: 0
  37. -- line height: 16
  38. -- part name: TextStream Example
  39. ----- HyperTalk script -----
  40. on mouseUp
  41.   global leftMargin,topMargin,rightMargin,bottomMargin
  42.   put 8 into leftMargin
  43.   put 4 into topMargin
  44.   put 10 into rightMargin
  45.   put 4 into bottomMargin
  46.   textstream field 1
  47.   get the result
  48.   if it is not empty then put it
  49. end mouseUp
  50.  
  51.  
  52.  
  53. -- part 3 (button)
  54. -- low flags: 00
  55. -- high flags: A003
  56. -- rect: left=299 top=300 right=322 bottom=438
  57. -- title width / last selected line: 0
  58. -- icon id / first selected line: 0 / 0
  59. -- text alignment: 1
  60. -- font id: 0
  61. -- text size: 12
  62. -- style flags: 0
  63. -- line height: 16
  64. -- part name: Show Pascal Source
  65. ----- HyperTalk script -----
  66. on mouseUp
  67.   set the visible of card field 1 to not the visible of card field 1
  68.   if the visible of card field 1 is true then
  69.     set the name of me to "Hide Pascal Source"
  70.   else set the name of me to "Show Pascal Source"
  71. end mouseUp
  72.  
  73.  
  74.  
  75. -- part contents for background part 16
  76. ----- text -----
  77. TEXTSTREAM XCMD version 2.0
  78. Kevin Calhoun
  79.  
  80. NOTE TO USERS OF VERSIONS EARLIER THAN 2.0:
  81. TextStream has been completely rewritten and the parameter list has changed.  Please read the complete documentation before replacing earlier versions in your stacks with TextStream 2.0.  The revisions were necessary to accommodate users who wanted to use TextStream to print mailing labels.
  82.  
  83. NOTE TO USERS OF LASERWRITER DRIVER VERSION 5.2:
  84. Under the following circumstance, a fatal system error can occur when using TextStream with Apple's LaserWriter Driver version 5.2.  Under Finder or under MultiFinder with background printing turned off, if the currently selected LaserWriter is not present on the network, the LaserWriter Driver may crash when TextStream tells it to close.  This crash does not occur if the currently selected LaserWriter is up and running.  The problem is in the LaserWriter driver; it was not present in version 5.0, and it is not present in the latest version at this writing, version 6.0.
  85.  
  86. The TextStream XCMD prints the contents of up to 16 HyperTalk containers using your printer's built-in font.  You have the option of telling TextStream how many characters wide you want your left and right margins to be and how many lines tall you want your top and bottom margins to be.
  87.  
  88. In case of an error, TextStream returns an error message as the Result.  Word 1 of this message will be "Error."   
  89.  
  90. TextStream works with both the LaserWriter and the ImageWriter.  It is intended primarily for use with eight-and-a-half by eleven inch sheets, although you can use it with smaller sheets by providing appropriate margin settings.
  91.  
  92. INVOKING TEXTSTREAM
  93.  
  94. TextStream firstContainer,<secondContainer>,<thirdContainer>,...,<sixteenthContainer>
  95.  
  96. Do not place quotation marks around the names of the containers.  Parameters given inside the brackets, such as  <notNeeded>,  are optional.
  97.  
  98. SETTING MARGINS
  99. TextStream looks in HyperTalk global variables for margin information.  The global variables leftMargin and rightMargin contain the number of characters to skip at the start and the end of each line.  The global variables topMargin and bottomMargin are the numbers of lines to skip at the top and the bottom of each page.
  100.  
  101. If these global variables are empty (or if they contain unreasonable numbers for margins), TextStream will default to 96 chars per line and 63 lines per page, which is as much as either the LaserWriter or the ImageWriter can fit on an eight-and-a-half by eleven inch page.
  102.  
  103. CHANGE HISTORY
  104. 7 March 1988 -- 1.0
  105. 7 April 1988 -- 1.2  Prints any container, not just fields.
  106. 15 March 1989 -- 1.5  Altered source code for compatibility with MPW Pascal 3.0.
  107. 18 June 1989 -- 2.0.  Complete rewrite.  No longer forces a page feed after printing each container.  No longer crashes after PrintField.
  108.  
  109. -- part contents for card part 1
  110. ----- text -----
  111. UNIT TextStreamUnit;
  112.  
  113. { TextStream XCMD ┬⌐ 1988-1989 by the Trustees of Dartmouth College }
  114. { Written by Kevin Calhoun }
  115.  
  116. { This source compatible with MPW Pascal 3.0 }
  117.  
  118. (*
  119. Pascal Textstream.p
  120. Link -m ENTRYPOINT Γêé
  121.      -o "YourFile" Γêé
  122.      -rt XCMD=9541 Γêé
  123.      -sn Main=TextStream Γêé
  124.      Textstream.p.o Γêé
  125.     "{Libraries}"interface.o Γêé
  126.     "{PLibraries}"Paslib.o Γêé
  127.     "{Libraries}"HyperXLib.o
  128. *)
  129.  
  130. {$R-}
  131.  
  132. INTERFACE
  133.   USES
  134.     Types,
  135.     Memory,
  136.     QuickDraw,
  137.     Windows,
  138.     ToolUtils,
  139.     Devices,
  140.     PrintTraps,
  141.     HyperXCmd;
  142.  
  143.   PROCEDURE EntryPoint (paramPtr : XCmdPtr);
  144.  
  145. IMPLEMENTATION
  146.  
  147.   PROCEDURE TextStream(paramPtr : XCMDPtr); FORWARD;
  148.   
  149.   PROCEDURE EntryPoint (paramPtr : XCmdPtr);
  150.   BEGIN
  151.     TextStream(paramPtr);
  152.   END;
  153.  
  154.   PROCEDURE PassReturnValue (paramPtr: XCMDPtr; errMsg : Str255);
  155.     { set theResult }
  156.   BEGIN
  157.     paramPtr^.returnValue := PasToZero(paramPtr, errMsg);
  158.   END;
  159.  
  160.   PROCEDURE GetMargins (paramPtr : XCmdPtr;
  161.                   VAR leftMargin, rightMargin, topMargin, bottomMargin : INTEGER);
  162.     VAR
  163.       hndl : Handle;
  164.  
  165.     FUNCTION HandleToNum (h : Handle) : LONGINT;
  166.       VAR
  167.         temp : Str255;
  168.     BEGIN
  169.       ZeroToPas(paramPtr, h^, temp);
  170.       HandleToNum := StrToNum(paramPtr, temp);
  171.     END;
  172.  
  173.   BEGIN
  174.     leftMargin := 0;
  175.     topMargin := 0;
  176.     rightMargin := 0;
  177.     bottomMargin := 0;
  178.     hndl := GetGlobal(paramPtr,'leftMargin');
  179.     IF paramPtr^.result = noErr THEN
  180.       BEGIN
  181.       leftMargin := HandleToNum(hndl);
  182.       DisposHandle(hndl);
  183.       END;
  184.     hndl := GetGlobal(paramPtr,'topMargin');
  185.     IF paramPtr^.result = noErr THEN
  186.       BEGIN
  187.       topMargin := HandleToNum(hndl);
  188.       DisposHandle(hndl);
  189.       END;
  190.     hndl := GetGlobal(paramPtr,'rightMargin');
  191.     IF paramPtr^.result = noErr THEN
  192.       BEGIN
  193.       rightMargin := HandleToNum(hndl);
  194.       DisposHandle(hndl);
  195.       END;
  196.     hndl := GetGlobal(paramPtr,'bottomMargin');
  197.     IF paramPtr^.result = noErr THEN
  198.       BEGIN
  199.       bottomMargin := HandleToNum(hndl);
  200.       DisposHandle(hndl);
  201.       END;
  202.   END;
  203.  
  204.   PROCEDURE StartPage (topMargin : INTEGER);
  205.     VAR
  206.       i : INTEGER;
  207.   BEGIN
  208.     PrCtlCall(iPrDevCtl, lPrPageOpen, 0, 0);  {init for new page}
  209.     FOR i := 1 TO topMargin DO
  210.       BEGIN
  211.       PrCtlCall(iPrDevCtl, lPrLineFeed, 0, 0);  {send CR and LF}
  212.       PrCtlCall(iPrDevCtl, lPrLFStd, 0, 0);
  213.       END;
  214.   END;
  215.   
  216.   PROCEDURE Text2Printer(paramPtr: XCMDPtr; textHndl: Handle; charsPerLine: INTEGER;
  217.                         linesPerPage: INTEGER; leftMargin: INTEGER; marginPtr: CharsPtr;
  218.                         topMargin: INTEGER; VAR linesUsed: INTEGER);
  219.   VAR
  220.     strLength: INTEGER;
  221.     charCount: INTEGER;
  222.     lineCount: INTEGER;
  223.     returnFlag: BOOLEAN;
  224.     hndlState: SignedByte;
  225.     charsToPrint: INTEGER;
  226.     leadingBlanks: INTEGER;
  227.     startLinePtr: Ptr;
  228.     endLinePtr: Ptr;
  229.     lineLength: INTEGER;
  230.     blankPos: INTEGER;
  231.  
  232.   BEGIN
  233.     strLength := StringLength(paramPtr, textHndl^);
  234.     IF strLength > 0 THEN
  235.       BEGIN
  236.       charCount := 0;
  237.       lineCount := linesUsed;
  238.       returnFlag := TRUE;
  239.       hndlState := HGetState(textHndl);
  240.       MoveHHi(textHndl);
  241.       HLock(textHndl);
  242.  
  243.       REPEAT
  244.         { print a page }
  245.         REPEAT
  246.           { print a line }
  247.           charsToPrint := charsPerLine;
  248.  
  249.           IF NOT returnFlag THEN
  250.           { if previous line did not end with a carriage return, skip leading blanks }
  251.             BEGIN
  252.             leadingBlanks := 0;
  253.             WHILE CharsHandle(textHndl)^^[charCount+leadingBlanks] = CHR($20) DO
  254.               leadingBlanks := leadingBlanks + 1;
  255.             charCount := charCount + leadingBlanks;
  256.             END;
  257.  
  258.           startLinePtr := Ptr(ORD4(textHndl^)+charCount);
  259.           endLinePtr := startLinePtr;
  260.           ScanToReturn(paramPtr, endLinePtr);
  261.           lineLength := ORD4(endLinePtr) - ORD4(startLinePtr) + 1;
  262.           IF lineLength <= charsToPrint + 1 THEN
  263.             BEGIN
  264.             returnFlag := TRUE;
  265.             charsToPrint := lineLength;
  266.             END
  267.           ELSE
  268.             BEGIN
  269.              { we have to wrap this line }
  270.             returnFlag := FALSE;
  271.             blankPos := charsToPrint + 1; { look for the last blank before the 98th char }
  272.             WHILE (CharsHandle(textHndl)^^[charCount+blankPos-1] <> CHR($20)) AND (blankPos > 1) DO
  273.               blankPos := blankPos - 1;
  274.               { if there are no blanks on the line, we print 96 chars -- }
  275.               { otherwise print up to and including last blank before 98th char }
  276.             IF blankPos > 1 THEN charsToPrint := blankPos;
  277.             END;
  278.  
  279.           IF leftMargin > 0 THEN PrCtlCall(iPrIOCtl, ORD(marginPtr), leftMargin, 0);
  280.  
  281.           PrCtlCall(iPrIOCtl, ORD(startLinePtr), charsToPrint, 0);  {send the line}
  282.           PrCtlCall(iPrDevCtl, lPrLineFeed, 0, 0);  {send CR and LF}
  283.           PrCtlCall(iPrDevCtl, lPrLFStd, 0, 0);
  284.           charCount := charCount + charsToPrint;
  285.           lineCount := lineCount + 1;
  286.         UNTIL (lineCount = linesPerPage) OR (charCount >= strLength);
  287.         IF lineCount = linesPerPage THEN
  288.           BEGIN
  289.           PrCtlCall(iPrDevCtl, lPrPageEnd, 0, 0);  {send end page}
  290.           StartPage(topMargin);
  291.           lineCount := 0;
  292.           END;
  293.       UNTIL charCount >= strLength;
  294.       HSetState(textHndl,hndlState);
  295.       linesUsed := lineCount;
  296.     END;  { if strLength > 0 }
  297.   END;
  298.  
  299.   PROCEDURE TextStream (paramPtr : XCmdPtr);
  300.     LABEL
  301.       99;
  302.     VAR
  303.       err : OSErr;
  304.       parameterCount : INTEGER;
  305.       leftMargin, rightMargin, topMargin, bottomMargin : INTEGER;
  306.       linesPerPage, lineLength : INTEGER;
  307.       marginHndl: CharsHandle;
  308.       marginPtr: CharsPtr;
  309.       i, curParam : INTEGER;
  310.       theText : Handle;
  311.       linesUsed: INTEGER;
  312.       watchHdl : CursHandle;
  313.       str : Str255;
  314.  
  315.   BEGIN
  316.     parameterCount := paramPtr^.paramCount;
  317.     IF parameterCount < 1 THEN
  318.       BEGIN
  319.       PassReturnValue(paramPtr,'TextStream XCMD 2.0, 18 June 1989, ┬⌐1988-1989 Dartmouth College');
  320.       GOTO 99;
  321.       END;
  322.     
  323.     err := noErr;
  324.     GetMargins(paramPtr, leftMargin, rightMargin, topMargin, bottomMargin);
  325.     lineLength := 96 - leftMargin - rightMargin;
  326.     IF lineLength <= 0 THEN GOTO 99;
  327.     linesPerPage := 63 - topMargin - bottomMargin;
  328.     IF linesPerPage <= 0 THEN GOTO 99;
  329.  
  330.     marginHndl := NIL;
  331.     IF leftMargin > 0 THEN
  332.       BEGIN
  333.       marginHndl := CharsHandle(NewHandle(leftMargin));
  334.       err := MemError;
  335.       IF err <> noErr THEN GOTO 99;
  336.       MoveHHi(Handle(marginHndl));
  337.       HLock(Handle(marginHndl));
  338.       FOR i := 0 TO (leftMargin - 1) DO marginHndl^^[i] := CHR($20);
  339.       marginPtr := marginHndl^;
  340.       END
  341.     ELSE marginPtr := NIL;
  342.  
  343.     PrDrvrOpen;
  344.     err := PrError;
  345.     IF err = noErr THEN
  346.       BEGIN
  347.       watchHdl := GetCursor(watchCursor);
  348.       SetCursor(watchHdl^^);
  349.   
  350.       PrCtlCall(iPrDevCtl, lPrReset, 0, 0);
  351.       err := PrError;
  352.       IF err = noErr THEN
  353.         BEGIN
  354.         StartPage(topMargin);
  355.         linesUsed := 0;
  356.         FOR curParam := 1 to parameterCount DO
  357.           BEGIN
  358.           theText := paramPtr^.params[curParam];
  359.           Text2Printer(paramPtr,theText,lineLength,linesPerPage,
  360.                       leftMargin,marginPtr,topMargin,linesUsed);
  361.           END;
  362.         
  363.         IF linesUsed < linesPerPage THEN
  364.           PrCtlCall(iPrDevCtl, lPrPageEnd, 0, 0);  { send end page }
  365.         PrCtlCall(iPrDevCtl, lPrDocClose, 0, 0);  { end doc }
  366.         END;
  367.       PrDrvrClose;
  368.       InitCursor;
  369.       END;
  370.  
  371.     IF marginHndl <> NIL THEN DisposHandle(Handle(marginHndl));
  372.     
  373.     99: IF err <> noErr then
  374.       BEGIN
  375.       NumToStr(paramPtr,err,str);
  376.       PassReturnValue(paramPtr, CONCAT('Error ',str));
  377.       END;
  378.  
  379.   END;
  380.   
  381. END.